<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2017/8/15
 * Time: 08:33
 */

namespace Admin\Controller;


use Admin\Model\Page;
use Common\Utils\Constants;
use Home\Logic\UserLogic;
use Think\Exception;
use Think\Log;

class UserController extends BaseController
{
    public function __construct()
    {
        parent::__construct(D('User'));
        $this->bcItemPush('用户列表', U('User/index'));
    }

    public function index()
    {
        $model = D('User');
        //构建查询条件
        $pagesize = 20;
        $id = I('id');
        $region = I('region');
        $systems_name = I('systems_name');
        $user_type = I('user_type');
        $is_referee = I('is_referee');
        $recommend_id = I('recommend_id');
        $last_one = I('last_one');
        $nickname = I('nickname');
        $pagenumber = I('pagenum', '');
        $is_register = I('is_register', '');
        if (empty($pagenumber) || $pagenumber <= 0)
            $pagenumber = 1;
        if ($nickname != '') {
            $map['t_user.nickname'] = array('like', '%' . $nickname . '%');
        }
        if ($id != '') {
            $map['t_user.id'] = $id;
        }
        if ($last_one != '') {
            $map['t_user.last_one'] = $last_one;
        }
        if ($region != '' && $region != '-1') {
            $map['t_user.region'] = $region;
        }
        if ($systems_name != '' && $systems_name != '-1') {
            $map['t_user.systems_name'] = $systems_name;
        }
        if ($user_type != '' && $user_type != '-1') {
            $map['t_user.user_type'] = $user_type;
        }
        if($is_referee !='' && $is_referee !='-1'){
            $map['t_user.is_referee'] = $is_referee;
        }
        if($recommend_id !='' && $recommend_id !='-1'){
            $map['t_user.recommend_id'] = $recommend_id;
        }
        if($is_register != '' && $is_register !='-1'){
            if($is_register == 1){
                $map['t_user.phone'] = array('exp','is not null');
            }else{
                $map['t_user.phone'] = array('exp','is null');
            }
        }
        $map['t_user.is_delete'] = 0;

        //查询
        $list = $model->getByConditionPage($map,$pagenumber,$pagesize);

        $recordcount = $model->where($map)->count();
        //封装分页对象
        $page = new Page($pagenumber, $recordcount, $pagesize);
        $page = (array)$page;//转换为数组

        //显示数据
        $this->assign('page', $page);
        $this->assign('pagenum', $pagenumber);
        $this->assign('list', $list);
        $this->assign('recordcount', $recordcount);
        $this->assign('is_register', $is_register);
        $this->assign('last_one', $last_one);
        $this->assign('nickname', $nickname);
        $this->assign('region', $region);
        $this->assign('systems_name', $systems_name);
        $this->assign('recommend_id', $recommend_id);
        $this->assign('user_type', $user_type);
        $this->assign('is_referee', $is_referee);
        $this->assign('id', $id);
        $this->display();
    }

    /**
     * 设置用户钻石界面
     */
    public function setUserIntegral(){
        $this->bcItemPush('设置用户钻石', U('User/setUserIntegral'));
        $this->display('set_integral');
    }

    public function setReferee(){
        $user_id = I('user_id');
        $user = D('User')->where(array('id'=>$user_id))->find();
        $this->assign('user',$user);
        $this->assign('user_id',$user_id);
        $this->display();
    }

//    public function delete(){
//        $user_id = I('user_id');
//        $user = D('User')->where(array('id'=>$user_id))->find();
//        if($user['is_delete'] == 1){
//            $this->ajaxReturn(array('success'=>false,'message'=>'对不起，该用户删除！'));
//        }
//        $user['openid'] = '';
//        $user['user_type'] = 0;
//    }

    /**
     * ajax获取用户信息
     */
    public function showUserInfo(){
        $user_id = I('user_id');
        $user = D('User')->where(array('id'=>$user_id))->find();
        if(!$user){
            $this->ajaxReturn(array('success'=>false));
        }
        $this->ajaxReturn(array('success'=>true,'userInfo'=>$user));
    }

    /**
     * ajax设置代理用户
     */
    public function setAgent(){
        $user_id = I('user_id');
        $logic = new \Admin\Logic\UserLogic();
        $status = $logic->setAgent($user_id);
        $success = false;
        switch ($status){
            case 0:
                $message = '设置失败！';
                break;
            case 1:
                $message = '设置成功！';
                $success = true;
                break;
            case 2:
                $message = '对不起，该用户已经是代理！';
                break;
        }
        $this->ajaxReturn(array('success'=>$success,'message'=>$message));
    }

    /**
     * ajax取消代理
     */
    public function cancelAgent(){
        $user_id = I('user_id');
        $logic = new \Admin\Logic\UserLogic();
        $status = $logic->cancelAgent($user_id);
        $success = false;
        switch ($status){
            case 0:
                $message = '取消代理失败！';
                break;
            case 1:
                $message = '取消代理成功！';
                $success = true;
                break;
            case 2:
                $message = '对不起，该用户不是代理！';
                break;
        }
        $this->ajaxReturn(array('success'=>$success,'message'=>$message));
    }


    /**
     * ajax设置裁判
     */
    public function ajaxSetReferee(){
        $user_id = I('user_id');
        $password = I('password');
        $re_password = I('re_password');
        $username = I('username');
        if(empty($username)){
            $this->ajaxReturn(array('success'=>false,'message'=>'登录名不能为空！'));
        }
        if(empty($password)){
            $this->ajaxReturn(array('success'=>false,'message'=>'登录密码不能为空！'));
        }
        if(empty($re_password)){
            $this->ajaxReturn(array('success'=>false,'message'=>'确认密码不能为空！'));
        }
        if($re_password != $password){
            $this->ajaxReturn(array('success'=>false,'message'=>'两次密码不一致！'));
        }
        $logic = new \Admin\Logic\UserLogic();
        $status = $logic->setReferee($user_id,$username,$password);
        $success = false;
        switch ($status){
            case 0:
                $message = '设置裁判失败！';
                break;
            case 1:
                $success = true;
                $message = '设置裁判成功！';
                break;
            case 2:
                $message = '该登录名已经被使用，请重新填写！';
                break;
        }
        $this->ajaxReturn(array('success'=>$success,'message'=>$message));
    }

    /**
     * ajax取消用户裁判权限
     */
    public function ajaxCancelReferee(){
        $user_id = I('user_id');
        $user = D('User')->where(array('id'=>$user_id))->find();
        if($user['is_referee'] != 1){
            $this->ajaxReturn(array('success'=>false,'message'=>'对不起，该用户不是裁判！'));
        }
        D('User')->where(array('id'=>$user_id))->setField('is_referee',0);
        D('Admin')->where(array('user_id'=>$user_id))->setField('is_use',0);
        $this->ajaxReturn(array('success'=>true,'message'=>'取消裁判成功！'));
    }

    /**
     * 更新字段
     */
    public function updateField()
    {
        $id = I('id');
        $value = I('value');
        $field = I('field');
        $where['id'] = ':id';
        $bind[':id'] = $id;
        $user = D('User')->where($where)->bind($bind)->find();
        $user[$field] = $value;
        $result = D('User')->where($where)->bind($bind)->setField($field,$value);
        if($result){
            $this->ajaxReturn(array('success'=>true,'message'=>'修改成功'));
        }else{
            $this->ajaxReturn(array('success'=>false,'message'=>'修改失败'));
        }
    }

    /**
     * 检查用户id
     */
    public function checkUserId(){
        $user_id = I('user_id');
        $user = D('User')->where(array('id'=>$user_id))->field('nickname,wechat_number,phone,integral')->find();
        if(!$user){
            $this->ajaxReturn(array('success'=>false,'message'=>'该用户不存在！'));
        }else{
            $this->ajaxReturn(array('success'=>true,'data'=>$user));
        }
    }

    /**
     * ajax修改用户钻石数目
     */
    public function ajaxSetUserIntegral(){
        $integral = I('integral');
        $user_id = I('user_id');
        $logic = new \Admin\Logic\UserLogic();
        $status = $logic->setUserIntegral($user_id,$integral);
        $success = false;
        switch ($status){
            case 0:
                $message = '设置用户钻石失败！';
                break;
            case 1:
                $success = true;
                $message = '设置用户钻石成功！';
                break;
            case 2:
                $message = '对不起，当前用户不存在！';
                break;
            case 3:
                $message = '请输入正确的钻石数目！';
                break;
        }
        $this->ajaxReturn(array('success'=>$success,'message'=>$message));
//        if($count == 0){
//            return 2;
//            $this->ajaxReturn(array('success'=>false,'message'=>'该用户不存在！'));
//        }
//        if(!is_numeric($integral) || $integral=0){
//            $this->ajaxReturn(array('success'=>false,'message'=>'请输入正确的钻石数目！'));
//        }
//        D('User')->where(array('id'=>$user_id))->setField('integral',$integral);
//        $this->ajaxReturn(array('success'=>true,'message'=>'设置成功！'));
    }

    /**
     * ajax删除测试用户
     */
    public function ajaxDeleteTestUser(){
        $model = M();
        $model->startTrans();
        try {
            foreach (Constants::TEST_USER_ARRAY as $key => $value) {
//                $this->ajaxReturn(array('success'=>false,'message'=>'删除测试用户失败'.$value));
                $model->table('t_user')->where(array('openid' => $value.''))->setField(array('phone'=> '','openid'=>'','is_first'=>1,'gold'=>0,'integral'=>0));
            }
            $model->commit();
            $this->ajaxReturn(array('success'=>true,'message'=>'删除测试用户成功'));
        }catch (Exception $e){
            Log::write('ajaxDeleteTestUser:---'.$e->getMessage());
            $model->rollback();
            $this->ajaxReturn(array('success'=>false,'message'=>'删除测试用户失败'));
        }
    }

    /**
     * ajax删除胜率榜数据
     */
    public function ajaxDeleteWinData(){
        $model = M();
        $model->startTrans();
        try {
//            $map['t_user.is_first'] = 0;
//            $model->table('t_user')->where($map)->setField(array('total_number'=>0,'win_number'=>0));
            $map['total_number'] = array('gt',0);
            $model->table('t_ranking')->where($map)->setField(array('total_number'=>0,'win_number'=>0));
            $model->commit();
            $this->ajaxReturn(array('success'=>true,'message'=>'删除胜率榜数据成功'));
        }catch (Exception $e){
            Log::write('ajaxDeleteWinData:---'.$e->getMessage());
            $model->rollback();
            $this->ajaxReturn(array('success'=>false,'message'=>'删除胜率榜数据失败'));
        }
    }

    /**
     * ajax清空代理的可提现金额
     */
    public function ajaxClearIncome(){
        $user_id = I('user_id');
        $logic = new \Admin\Logic\UserLogic();
        $status = $logic->clearUserIncome($user_id);
        $success = false;
        switch ($status){
            case 0:
                $message = '清空失败！';
                break;
            case 1:
                $success = true;
                $message = '清空成功！';
                break;
            case 2:
                $message = '对不起，当前用户不存在！';
                break;
            case 3:
                $message = '当前代理的可提现金额为0！';
                break;
        }
        $this->ajaxReturn(array('success'=>$success,'message'=>$message));
    }

    /**
     * 清空用户浏览量
     */
    public function ajaxClearPageviews(){
        $user_id = I('user_id');
        $logic = new \Admin\Logic\UserLogic();
        $status = $logic->clearPageviews($user_id);
        $success = false;
        switch ($status){
            case 0:
                $message = '清空失败！';
                break;
            case 1:
                $success = true;
                $message = '清空成功！';
                break;
            case 2:
                $message = '对不起，当前用户不存在！';
                break;
            case 3:
                $message = '当前用户的浏览量为0！';
                break;
        }
        $this->ajaxReturn(array('success'=>$success,'message'=>$message));
    }

    /**
     * 显示用户推广页的浏览记录
     */
    public function showBrowseLog(){
        $user_id = I('user_id');
        $nickname = D('User')->where(array('id'=>$user_id))->getField('nickname');
        $map['user_id'] = $user_id;
        $model = D('BrowseLog');
        $pagenumber = I('pagenum', '');
        $pagesize = 20;
        if (empty($pagenumber) || $pagenumber <= 0)
            $pagenumber = 1;
        //查询
        $list = $model->getByConditionPage($map,$pagenumber,$pagesize);

        $recordcount = $model->where($map)->count();
        //封装分页对象
        $page = new Page($pagenumber, $recordcount, $pagesize);
        $page = (array)$page;//转换为数组

        //显示数据
        $this->assign('page', $page);
        $this->assign('pagenum', $pagenumber);
        $this->assign('recordcount', $recordcount);
        $this->assign('list', $list);
        $this->assign('nickname', $nickname);
        $this->bcItemPush('浏览记录', U('User/showBrowseLog'));
        $this->display('browse_log');
    }



}